package com.biye.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.base.common.edu.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

@Controller
public class RedisExampleController {

    @Autowired
    JedisCluster jedisCluster;

    //遍历当前redis中的key
    @RequestMapping(value="redis1",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RErgodic(){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="获取Key列表失败";

        System.out.println(jedisCluster.get("Master_QQ_random")==null?"值不不存在":"值存在");
        TreeSet<String> keys = new TreeSet<String>();
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
        for(Iterator<JedisPool> it = clusterNodes.values().iterator(); it.hasNext();) {
            JedisPool pool = it.next();
            Jedis jedis = pool.getResource();
//            jedis.auth("basic@5678");    // redis可以不加密码，但不要暴露在公网
            try {
                keys.addAll(jedis.keys("*"));
            } catch (Exception e) {
                throw e;
            } finally {
                jedis.close();
            }
        }
//        System.out.println(keys);

//        Set<String> set = jedisCluster.keys("*");
//        System.out.println("a=" + set);
        if(keys!=null&&keys.size()>0){

            for(String keyname:keys){
//                System.out.println(keyname + "=" + jedisCluster.get(keyname));
                json.put(keyname, jedisCluster.get(keyname));
            }
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    @RequestMapping(value="/common/os",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject printYourOs(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        // 获取登录的操作系统信息
        String browserName = RequestUtil.getBrowserName(request) == null ? "未使用浏览器" : RequestUtil.getBrowserName(request);
        String browserVersion;
        try{
            browserVersion = RequestUtil.getBrowserVersion(request) != null ? RequestUtil.getBrowserVersion(request) : "未使用浏览器即未知版本";
        }catch (NullPointerException e){
            browserVersion = "浏览器版本未知";
        }
        String osName = RequestUtil.getOsName(request) == null ? "操作系统未知" : RequestUtil.getOsName(request);
        String ipAddress = RequestUtil.getIpAddress(request);
        String info_OS = "[OS-Name：" + osName + "][Browser-Name：" + browserName + "][Browser-Version：" + browserVersion + "]";
        jsonObject.put("os", info_OS);
        jsonObject.put("ip", ipAddress);
        return jsonObject;
    }

}
